$(document).ready(function () {

    executeLoadtimeActions();
    loadCheckboxBehavior();
    loadAddButtonBehavior();
    loadEditAnchorBehavior();
    loadDeleteButtonBehavior();

});

function executeLoadtimeActions() {

    $('table.data-table tbody tr:odd').addClass('odd');
    $('table.data-table tbody tr:even').addClass('even');

    if (employmentContracts == 0) {
        $('#tblEmploymentContracts .check').hide();
    }

}

function loadCheckboxBehavior() {

    $("#checkAll").click(function () {
        if ($("#checkAll:checked").attr('value') == 'on') {
            $(".checkbox").attr('checked', 'checked');
        } else {
            $(".checkbox").removeAttr('checked');
        }
    });

    $(".checkbox").click(function () {

        $("#checkAll").removeAttr('checked');

        if (($(".checkbox").length - 1) == $(".checkbox:checked").length) {
            $("#checkAll").attr('checked', 'checked');
        }

        if ($(".checkbox:checked").length > 0 && $(".checkbox").length > 1) {
            $('#btnDeleteEmploymentContract').removeAttr('disabled');
        } else {
            $('#btnDeleteEmploymentContract').attr('disabled', 'disabled');
        }

    });

}

function loadAddButtonBehavior() {
    $('#btnAddEmploymentContract').click(function () {
        window.location.replace(addEmploymentContractUrl);
    });
}

function loadEditAnchorBehavior() {
    $('#employmentContractList a.editLink').click(function (event) {
        event.preventDefault();

        var row = $(this).closest("tr");
        var employmentContractId = row.find('input.checkbox:first').val();

        window.location.replace(editEmploymentContractUrlSegment + employmentContractId);
    });
}

function loadDeleteButtonBehavior() {

    if ($(".checkbox:checked").length == 0) {
        $('#btnDeleteEmploymentContract').attr('disabled', 'disabled');
    }

    $('#btnDeleteEmploymentContract').click(function () {
        console.log('aa');
        $('#frmEmpDelEmploymentContracts').submit(function () {
            $('#deleteConfModal').dialog('open');
            return false;
        });
    });

    $('#dialogDeleteBtn').click(function () {
        document.frmEmpDelEmploymentContracts.submit();
    });
}

function _clearErrorMessages() {
    $('.errorHolder').each(function () {
        $(this).empty();
    });
}


